/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import controllers.DatabaseController;
import controllers.MainWindowController;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.UIManager;
import listcontrol.LocalFileList;
import listcontrol.RemoteFileList;

/**
 *
 * @author Márcio Costa Junior - University of Sao Paulo
 */
public class MainWindow extends javax.swing.JFrame {

    private ArrayList<DownloadItem> downloadsList;       
    private MainWindowController controller;
    
    /**
     * Creates new form MainWindow
     */
    public MainWindow(DatabaseController dbController) {
        initComponents();
        this.setTitle("Distributed File Exchange");
        this.setLocation(300, 100);
        this.setSize(600, 600);
        
        controller = new MainWindowController(this, dbController);
        
        downloadsList = new ArrayList<>();
        

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        searchButton = new javax.swing.JButton();
        shareButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        scrollPane = new javax.swing.JScrollPane();
        downloadsPanel = new javax.swing.JPanel();
        optionsButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(226, 245, 246));

        searchButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        searchButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/magnifying_glass_icon.png"))); // NOI18N
        searchButton.setToolTipText("Search Files");
        searchButton.setPreferredSize(new java.awt.Dimension(20, 20));
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        shareButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        shareButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/share_icon.png"))); // NOI18N
        shareButton.setToolTipText("My Shared Files");
        shareButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                shareButtonActionPerformed(evt);
            }
        });

        searchField.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N

        scrollPane.setBackground(new java.awt.Color(255, 255, 255));
        scrollPane.setBorder(null);
        scrollPane.setToolTipText("");

        downloadsPanel.setLayout(new javax.swing.BoxLayout(downloadsPanel, javax.swing.BoxLayout.Y_AXIS));
        scrollPane.setViewportView(downloadsPanel);

        optionsButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        optionsButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/settings_icon.png"))); // NOI18N
        optionsButton.setToolTipText("My Shared Files");
        optionsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                optionsButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchField, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(shareButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(optionsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchField, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shareButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(optionsButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Search button 
    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
                //Apenas passa o texto para a próxima janela - SearchWindow
                SearchWindow search = new SearchWindow(searchField.getText(), controller);
                search.setVisible(true);
    }//GEN-LAST:event_searchButtonActionPerformed

    //Share button
    private void shareButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_shareButtonActionPerformed
                //Abre a janela de seleção de arquivos para a pasta de compartilhamento
                ShareWindow share = new ShareWindow(this.controller);
                this.setVisible(false);
                share.setVisible(true);
    }//GEN-LAST:event_shareButtonActionPerformed

    private void optionsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optionsButtonActionPerformed
        // TODO add your handling code here:
        new ConfigWindow().setVisible(true);
    }//GEN-LAST:event_optionsButtonActionPerformed

    
    //Atualiza a barra de progresso e a velocidade de download de todos os itens
    public void updateDownloadStatus(){
        for(int i = 0; i < downloadsList.size(); i++){
            downloadsList.get(i).refresh();
        }
    }
    
    //Adiciona um novo download
    public void addDownloadItem(DownloadItem item){        
        downloadsList.add(item);                        
        this.repaint();
    }
    
    //Remove um download da lista       
    public void removeDownload(int id){
        downloadsPanel.remove(downloadsList.get(id));
        downloadsPanel.repaint();
        
        downloadsList.remove(id);   //  USAR equivalente a DICTIONARY
  
        int size = downloadsList.size();
        
        if(size == 0){
            downloadsPanel.add(new JTextField("No current downloads."));
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainWindow(null).setVisible(true);
            }
        });
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel downloadsPanel;
    private javax.swing.JButton optionsButton;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    private javax.swing.JButton shareButton;
    // End of variables declaration//GEN-END:variables

}
